//
//  SLDataPickViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/30.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLDataPickViewController.h"
#import "STPopup.h"
#import "DateTools.h"

@interface SLDataPickViewController ()

@property (nonatomic, strong) UIDatePicker *datePickView;

@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation SLDataPickViewController

- (instancetype)init
{
  if (self = [super init]) {
    self.contentSizeInPopup = CGSizeMake(SCREEN_WIDTH-80, 200*MIX_H);
    self.landscapeContentSizeInPopup = CGSizeMake(307*MIX_W, 110*MIX_H);
  }
  return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
  
  UILabel *titleLabel = [UILabel labelWithFontSize:16*MIX_W textColor:SLYellowColor text:@"选择日期"];
  [self.view addSubview:titleLabel];
  [self.view addSubview:self.confirmBtn];
  [self.view addSubview:self.cancelBtn];
  [self.view addSubview:self.datePickView];
  
  CGFloat pading = 8.f;
  
  [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.top.equalTo (self.view).offset(pading*MIX_W);
  }];
  
  [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(titleLabel);
    make.left.equalTo(self.view).offset(pading*MIX_W);
    make.size.mas_equalTo(CGSizeMake(40*MIX_W, 24*MIX_W));
  }];
  
  [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.cancelBtn);
    make.size.equalTo(self.cancelBtn);
    make.right.equalTo(self.view).offset(-pading*MIX_W);
  }];
  
  
  [self.datePickView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.top.equalTo(self.cancelBtn.mas_bottom).offset(pading*MIX_W);
    make.width.equalTo(self.view);
    make.bottom.equalTo(self.view);
  }];
    // Do any additional setup after loading the view.
}


- (void)selfDissmiss{
  [self dismissViewControllerAnimated:NO completion:nil];
}


- (void)didSelectDate{
  NSDate *newdate = self.datePickView.date;
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  NSString *currentOlderOneDateStr = [dateFormatter stringFromDate:newdate];
  if (self.datePickBlock != nil) {
      self.datePickBlock(currentOlderOneDateStr);
  }
  [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)returnDate:(datePickBlock)block{
  self.datePickBlock = block;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIDatePicker *)datePickView{
  if (!_datePickView) {
    _datePickView = [[UIDatePicker alloc] init];
    _datePickView.datePickerMode = UIDatePickerModeDate;
  }
  return _datePickView;
}


- (UIButton *)cancelBtn{
  if (!_cancelBtn) {
    _cancelBtn = [UIButton buttonWithTitle:@"取消"];
    [_cancelBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _cancelBtn.layer.borderColor = SLYellowColor.CGColor;
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14*MIX_W];
    _cancelBtn.layer.borderWidth = 0.5;
    [_cancelBtn addTarget:self action:@selector(selfDissmiss) forControlEvents:UIControlEventTouchUpInside];
  }
  return _cancelBtn;
}

- (UIButton *)confirmBtn{
  if (!_confirmBtn) {
    _confirmBtn = [UIButton buttonWithTitle:@"确认"];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:14*MIX_W];
    _confirmBtn.backgroundColor = SLYellowColor;
    [_confirmBtn addTarget:self action:@selector(didSelectDate) forControlEvents:UIControlEventTouchUpInside];
  }
  return _confirmBtn;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
